package com.yanqu.road.server.manger.activity.guguoxunbao;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.guguoxunbao.GuGuoXunBaoClueComment;
import com.yanqu.road.entity.activity.guguoxunbao.GuGuoXunBaoPlace;
import com.yanqu.road.entity.activity.guguoxunbao.GuGuoXunBaoTreasure;
import com.yanqu.road.entity.activity.guguoxunbao.GuGuoXunBaoUserData;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.GuGuoXunBaoBusiness;
import com.yanqu.road.pb.activity.GuGuoXunBaoProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.guguoxunbao.GuGuoXunBaoModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GuGuoXunBaoMgr extends TempMgr {

    private static ActivityInfo activityInfo;

    //线索评价
    private static Map<Integer, GuGuoXunBaoClueComment> clueCommentMap;
    //古国地点
    private static Map<Integer, GuGuoXunBaoPlace> placeMap;
    //古国宝物
    private static Map<Integer, GuGuoXunBaoTreasure> treasureMap;

    //骆驼id
    public static int CAMEL_ID = 120351;
    //探索初始位置
    public static int INIT_EXPLORE_PLACE_ID = 105;
    //探索线索个数
    static List<Integer> randomNum;
    //探索线索个数的权重
    static List<Integer> randomWeight;



    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload guguoxunbao activity start");

        // 获取活动
        ActivityInfo tmpActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.GuGuoXunBaoActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            return;
        }
        tmpActivityInfo = openActivityInfoList.get(0);

        Map<Integer, GuGuoXunBaoClueComment> tempCommentMap = GuGuoXunBaoBusiness.getClueCommentMap(tmpActivityInfo.getActivityId());
        Map<Integer, GuGuoXunBaoPlace> tempPlaceMap = GuGuoXunBaoBusiness.getPlaceMap(tmpActivityInfo.getActivityId());
        Map<Integer, GuGuoXunBaoTreasure> tempTreasureMap = GuGuoXunBaoBusiness.getTreasureMap(tmpActivityInfo.getActivityId());

        // 活动配置
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());
        if(activityConfigMap != null){
            if(activityConfigMap.containsKey("XUN_BAO_SCORE_ITEM_ID")){
                CAMEL_ID = activityConfigMap.get("XUN_BAO_SCORE_ITEM_ID").getIntValue();
            } else if(activityConfigMap.containsKey(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName())){
                CAMEL_ID = activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue();
            }
            if(activityConfigMap.containsKey("XUN_BAO_INIT_PLACE_ID")){
                INIT_EXPLORE_PLACE_ID = activityConfigMap.get("XUN_BAO_INIT_PLACE_ID").getIntValue();
            }
            if(activityConfigMap.containsKey("XUN_BAO_EXPLORE_CLUE_WEIGHTS")){
                String str = activityConfigMap.get("XUN_BAO_EXPLORE_CLUE_WEIGHTS").getValue();
                List<String> perGroup = StringUtils.stringToStringList(str, "\\|");
                List<Integer> valList = new ArrayList<>();
                List<Integer> weightList = new ArrayList<>();
                for(String group : perGroup){
                    List<Integer> values  = StringUtils.stringToIntegerList(group, ",");
                    if(values.size() == 2){
                        valList.add(values.get(0));
                        weightList.add(values.get(1));
                    }
                }
                randomNum = valList;
                randomWeight = weightList;
            }else {
                randomNum = new ArrayList<>();
                randomWeight = new ArrayList<>();
                getLogger().error("activityId {} EXPLORE_CLUE_WEIGHTS Config errer");
            }
        }

        clueCommentMap = tempCommentMap;
        placeMap = tempPlaceMap;
        treasureMap = tempTreasureMap;
        activityInfo = tmpActivityInfo;
        reloadPlayerData();
        getLogger().info("reload guguoxunbao activity end");
    }

    private static void reloadPlayerData() {
        if (ActivityMgr.activityInShowTime(activityInfo)) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                if (SystemOpenMgr.systemOpen(player, eSystemId.GuGuoXunBaoActivity.getValue())) {
                    GuGuoXunBaoModule module = player.getModule(GuGuoXunBaoModule.class);
                    if (module.getUserData() == null || module.getUserData().getActivityId() != activityInfo.getActivityId()) {
                        module.initUserData();
                        module.syncUserData();
                    }
                }
            }
        }
    }

    public static GuGuoXunBaoProto.GuGuoXunBaoConfigDataTemp.Builder getConfigDataTempBuilder(String language) {
        GuGuoXunBaoProto.GuGuoXunBaoConfigDataTemp.Builder builder = GuGuoXunBaoProto.GuGuoXunBaoConfigDataTemp.newBuilder();
        for(Map.Entry<Integer, GuGuoXunBaoPlace> placeEntry : placeMap.entrySet()){
            GuGuoXunBaoProto.GuGuoXunBaoPlaceTemp.Builder placeBuilder = GuGuoXunBaoProto.GuGuoXunBaoPlaceTemp.newBuilder();
            GuGuoXunBaoPlace place = placeEntry.getValue();
            placeBuilder.setId(place.getId());
            placeBuilder.setName(ServerLanguageMgr.getContent(place.getName(), language));
            placeBuilder.setDesc(ServerLanguageMgr.getContent(place.getDesc(), language));
            placeBuilder.setTreasureId(place.getTreasureId());
            placeBuilder.setNum(place.getClueNum());
            List<String> normalRewardList = new ArrayList<>();
            List<Integer> weightList = new ArrayList<>();
            extractPlaceReward(place, normalRewardList, weightList);
            placeBuilder.setReward(StringUtils.listToString(normalRewardList, ";"));
            builder.addPlace(placeBuilder);
        }
        for(Map.Entry<Integer, GuGuoXunBaoTreasure> treasureEntry : treasureMap.entrySet()){
            GuGuoXunBaoTreasure treasure = treasureEntry.getValue();
            GuGuoXunBaoProto.GuGuoXunBaoTreasureTemp.Builder treasureBuilder = GuGuoXunBaoProto.GuGuoXunBaoTreasureTemp.newBuilder();
            treasureBuilder.setId(treasure.getId());
            treasureBuilder.setName(ServerLanguageMgr.getContent(treasure.getName(), language));
            treasureBuilder.setDesc(ServerLanguageMgr.getContent(treasure.getDesc(), language));
            treasureBuilder.setValue(treasure.getValue());
            builder.addTreasure(treasureBuilder);
        }
        return builder;
    }

    public static GuGuoXunBaoProto.GuGuoXunBaoUserDataTemp.Builder getUserDataBuilder(GuGuoXunBaoUserData userData) {
        GuGuoXunBaoProto.GuGuoXunBaoUserDataTemp.Builder builder = GuGuoXunBaoProto.GuGuoXunBaoUserDataTemp.newBuilder();
        for(Map.Entry<Integer, Integer> entry : userData.getCurProgress().entrySet()){
            GuGuoXunBaoProto.StateClueDataTemp.Builder clueBuilder = GuGuoXunBaoProto.StateClueDataTemp.newBuilder();
            clueBuilder.setPlaceId(entry.getKey());
            clueBuilder.setClueNum(entry.getValue());
            builder.addClue(clueBuilder);
        }
        for(Map.Entry<Integer, Long> longEntry : userData.getTreasureMap().entrySet()){
            GuGuoXunBaoProto.TreasureDataTemp.Builder treasureBuilder = GuGuoXunBaoProto.TreasureDataTemp.newBuilder();
            treasureBuilder.setTreasureId(longEntry.getKey());
            treasureBuilder.setNum(longEntry.getValue());
            builder.addTreasure(treasureBuilder);
        }
        builder.setCurPlaceId(userData.getCurPlaceId());
        return builder;
    }

    /**
     * 探索评价
     */
    public static String getClueComment(int clueNum, String language) {
        for(Map.Entry<Integer, GuGuoXunBaoClueComment> entry : getClueCommentMap().entrySet()){
            GuGuoXunBaoClueComment clueComment = entry.getValue();
            if(clueComment.getNum().size() == 2 && clueNum >= clueComment.getNum().get(0) && clueNum <= clueComment.getNum().get(1)){
                return  ServerLanguageMgr.getContent(clueComment.getDesc(), language);
            }else if(clueComment.getNum().size() == 1 && clueNum == clueComment.getNum().get(0)){
                return  ServerLanguageMgr.getContent(clueComment.getDesc(), language);
            }
        }
        return "";
    }

    public static void extractPlaceReward(GuGuoXunBaoPlace placeInfo, List<String> normalRewardList, List<Integer> weightList) {
        List<String> normalRewardWeightList = StringUtils.stringToStringList(placeInfo.getRewards(), "\\|");
        for(String str : normalRewardWeightList){
            List<String> rewardWeight = StringUtils.stringToStringList(str, ";");
            if(rewardWeight.size() == 2) {
                normalRewardList.add(rewardWeight.get(0));
                weightList.add(Integer.valueOf(rewardWeight.get(1)));
            }
        }
    }

    /**
     * 是否在清空道具时间内
     */
    public static boolean isActivityInClearTime() {
        return ActivityMgr.activityInRewardPeriod(activityInfo);
    }

    /**
     * 获取展示中的活动ID（0表示没活动）
     */
    public static int getShowTimeActivityId() {
        return activityInfo == null ? 0 : activityInfo.getActivityId();
    }

    public static boolean activityInTime(){
        return ActivityMgr.activityInTime(activityInfo);
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static Map<Integer, GuGuoXunBaoClueComment> getClueCommentMap() {
        return clueCommentMap;
    }

    public static Map<Integer, GuGuoXunBaoPlace> getPlaceMap() {
        return placeMap;
    }

    public static Map<Integer, GuGuoXunBaoTreasure> getTreasureMap() {
        return treasureMap;
    }

    public static List<Integer> getRandomNum() {
        return randomNum;
    }

    public static List<Integer> getRandomWeight() {
        return randomWeight;
    }
}
